﻿#include "myshared.h"
#include <QMessageBox>
#include <QFile>
#include <QDomDocument>
#include <QDomProcessingInstruction>
#include <QDomElement>
#include <QTextStream>
#include <QDebug>

MyShared::MyShared()
{
    QMessageBox::information(nullptr, "Hint", "This is my sharedDll, to create a empty xml!");
}

void MyShared::createXML(QString filePath)
{
    QFile file(filePath);
    if(file.exists())
    {
        cout << "file exist";
        return;
    }
    else
    {
        //不存在才创建
        bool isOk = file.open(QIODevice::WriteOnly);
        if(isOk)
        {
            //创建xml文档对象
            QDomDocument doc;
            //创建xml头部格式 <?xml version='1.0' encoding='utf-8'?>
            QDomProcessingInstruction ins;
            ins = doc.createProcessingInstruction("xml", "version=\'1.0\' encoding=\'utf-8\'");
            //追加元素
            doc.appendChild(ins);

            //根节点元素
            QDomElement root = doc.createElement("Root");
            doc.appendChild(root);

            //文本流关联文件
            QTextStream stream(&file);
            //4 缩进字符
            doc.save(stream, 4);

            file.close();

        }
        else
        {
            cout << "WriteOnly error";
            return;
        }
    }
}
